package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.mapper.Btype_typeMapper;
import com.vehicle.pojo.Btype;
import com.vehicle.pojo.Btype_type;
import com.vehicle.service.BtypeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping({"/btype"})
public class BtypeController {
    @Autowired
    private BtypeService btypeService;

    private Logger logger = Logger.getLogger(this.getClass());

    public BtypeController() {
    }

    @RequestMapping({"/btypes"})
    @ResponseBody
    public DataGridView list(Btype btype) {
        DataGridView btypeBypage = this.btypeService.findBtypeBypage(btype);
        return btypeBypage;
    }
    //返回所有往来单位 用于下拉菜单数据
    @RequestMapping(value = "/allBtype")
    @ResponseBody
    public DataGridView allBtype(){
        return btypeService.findAllBtype();
    }


    //返回汽车厂商的下来菜单
    @RequestMapping("/carBtype")
    @ResponseBody
    public List<Btype> carBtype(){
        return btypeService.findBtypeWithCar();
    }

    //往来单位类型集合
    @ResponseBody
    @RequestMapping("/btype_type")
    public  List<Btype_type> allBtype_type(){
        return btypeService.findBtype_bypes();
    }

    //增加往来单位
    @ResponseBody
    @RequestMapping("/addBtype")
    public ResultDto addBtype(Btype btype){
        ResultDto resultDto=new ResultDto();
        try {
            btypeService.addBtype(btype);
            resultDto.setCode(200);
            resultDto.setMsg("增加成功");
            resultDto.setData(btype);
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("增加失败");
            e.printStackTrace();
        }
        return resultDto;
    }

    //修改往来单位
    @RequestMapping("/editBtype")
    @ResponseBody
    public ResultDto editBtype(Btype btype){
        ResultDto resultDto=new ResultDto();
        logger.info(btype);
        try {
            btypeService.editBtype(btype);
            resultDto.setMsg("修改成功");
            resultDto.setCode(200);
            resultDto.setData(btype);
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }
    //单个删除
    @RequestMapping("/delete")
    @ResponseBody
    public ResultDto deleteBtype(Integer btypeid){
        ResultDto resultDto=new ResultDto();
        try {
            btypeService.updateStatus(btypeid);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    //批量删除
    @RequestMapping("/deleteBatch")
    @ResponseBody
    public ResultDto deleteBtypeBatch(@RequestParam("btypeids[]") Integer[] btypeids){
        ResultDto resultDto=new ResultDto();
        try {
            btypeService.updateStatusBatch(btypeids);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
            e.printStackTrace();
        }
        return resultDto;
    }

}